/*
 * CDBTableModel.java
 *
 * Copyright (c) 2009 Giles Millward (Manigra). All rights reserved.
 *
 * This file is part of JRogueDPS.
 *
 * JRogueDPS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JRogueDPS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JRogueDPS.  If not, see <http://www.gnu.org/licenses/>.
 */
package geardatabase;

import java.util.ArrayList;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

/**
 *
 * @author Mani
 */
public class CDBTableModel extends AbstractTableModel {

    ArrayList<String> m_columnNameList = new ArrayList<String>();
    ArrayList<ArrayList> m_arrayList = new ArrayList<ArrayList>();

    public void cleanRows()
    {
        for (int i = 0; i < m_arrayList.size(); i++)
        {
            m_arrayList.get(i).clear();
        }
    }
    
    public void addColumnData(ArrayList<CItem> p_arrayList, int p_column)
    {
        m_arrayList.get(p_column).addAll(p_arrayList);
        this.fireTableDataChanged();
    }

    public void setWidth(JTable p_table, int p_width)
    {
        TableColumnModel l_model = p_table.getColumnModel();

        for (int i = 0; i < m_columnNameList.size(); i++)
        {
            TableColumn l_col = l_model.getColumn(i);
            l_col.setPreferredWidth(p_width);
            l_col.setWidth(p_width);
        }

        this.fireTableDataChanged();
    }

    public void refreshTable()
    {
        this.fireTableDataChanged();
    }

    public void addColumn(String p_columnName)
    {
        m_columnNameList.add(p_columnName);
        m_arrayList.add(new ArrayList());
        this.fireTableStructureChanged();

    }

    public int getColumnCount() {
        return m_columnNameList.size();
    }

    public int getRowCount() {
        int l_maxRowCount = 0;
        for (int i = 0; i < m_columnNameList.size(); i++)
        {
            l_maxRowCount = Math.max(l_maxRowCount, m_arrayList.get(i).size());
        }
        return l_maxRowCount;
    }

    @Override
    public String getColumnName(int col) {
        return m_columnNameList.get(col);
    }

    public Object getValueAt(int row, int col) {
        if (m_arrayList.get(col).size() <= row)
        {
            return null;
        }

        return m_arrayList.get(col).get(row);
    }

    /*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  
     */
    @Override
    public Class getColumnClass(int c) {
        return CItem.class;
    }

    /*
     * Don't need to implement this method unless your table's
     * editable.
     */
    @Override
    public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        return false;
    }

/*
     * Don't need to implement this method unless your table's
     * data can change.
     */
    @Override
    public void setValueAt(Object value, int row, int col) {
        m_arrayList.get(col).set(row, value);
        fireTableCellUpdated(row, col);
    }

}
